<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item class="mr" label="设备名称" prop="pName">
        <el-input
          v-model="queryParams.pName"
          placeholder="请输入"
          clearable
          style="width: 220px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="设备数量" prop="devCount">
        <el-input
          v-model="queryParams.devCount"
          placeholder="请输入"
          clearable
          style="width: 220px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="发布状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          placeholder="请选择"
          style="width: 220px"
        >
          <el-option
            v-for="{ label, value } in sys_common_status"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['alarm:info:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['alarm:info:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['alarm:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:info:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['alarm:info:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <!-- <CardList :infoList="infoList" :dictList="dictList" v-loading="loading" /> -->
    <div id="card-box">
      <el-row :gutter="12" style="width: 100%" v-loading="loading">
        <el-col v-for="(row, index) in infoList" :key="row.id" :span="6">
          <div class="card">
            <div class="card-header">
              <div class="card-header-icon">
                <el-icon><Odometer /></el-icon>
              </div>
              <div class="card-header-content">
                <div class="card-header-title">{{ row.pName }}</div>
                <div class="card-header-desc">
                  {{ row.description }}
                </div>
              </div>
            </div>
            <div class="card-body">
              <div class="card-body-item">
                <div class="card-body-item-title">设备数量</div>
                <div class="card-body-item-number">{{ row.devCount }}</div>
              </div>
              <div class="card-body-item">
                <div class="card-body-item-title">发布状态</div>
                <div class="card-body-item-status">
                  <dict-tag :options="sys_common_status" :value="row.status" />
                </div>
              </div>
              <div class="card-body-item">
                <div class="card-body-item-title">产品类型</div>
                <div class="card-body-item-type">
                  <dict-tag :options="sys_product_type" :value="row.pType" />
                </div>
              </div>
            </div>
            <div class="card-bottom">
              <div
                class="card-bottom-icon"
                title="详细"
                @click="handleView(row)"
              >
                <el-icon><View /></el-icon>
              </div>
              <div
                class="card-bottom-icon"
                title="编辑"
                @click="handleUpdate(row)"
              >
                <el-icon><EditPen /></el-icon>
              </div>
              <div
                class="card-bottom-icon"
                title="删除"
                @click="handleDelete(row, index)"
              >
                <el-icon><Delete /></el-icon>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        :model="form"
        :rules="rules"
        ref="environmentInfoRef"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="pName">
              <el-input v-model="form.pName" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="发布状态" prop="status">
              <el-select
                v-model="form.status"
                clearable
                placeholder="请选择"
                style="width: 260px"
              >
                <el-option
                  v-for="{ label, value } in sys_common_status"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备数量" prop="devCount">
              <el-input-number
                v-model="form.devCount"
                placeholder="请输入"
                :min="0"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="产品类型" prop="pType">
              <el-select
                v-model="form.pType"
                clearable
                placeholder="请选择"
                style="width: 260px"
              >
                <el-option
                  v-for="{ label, value } in sys_product_type"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介" prop="description">
              <el-input
                type="textarea"
                v-model="form.description"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 总览详细 -->
    <el-dialog title="总览详细" v-model="openView" width="500px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称：">{{ form.pName }}</el-form-item>
            <el-form-item label="产品类型：">
              <dict-tag :options="sys_product_type" :value="form.pType" />
              {{ form.endTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备数量：">
              {{ form.devCount }}
            </el-form-item>
            <el-form-item label="发布状态：">
              <dict-tag :options="sys_common_status" :value="form.status" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介：">
              {{ form.description }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="environmentInfo">
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo
} from '@/api/environment/info'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sys_common_status } = proxy.useDict('sys_common_status') //发布状态
const { sys_product_type } = proxy.useDict('sys_product_type') //产品类型

const infoList = ref([])
const open = ref(false)
const loading = ref(true)
const openView = ref(false)
const title = ref('')
const total = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    pName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询总览列表 */
function getList() {
  loading.value = true
  listInfo(queryParams.value).then((response) => {
    infoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('environmentInfoRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加设备信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const infoId = row.id
  getInfo(infoId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改设备信息'
  })
}

/** 删除按钮操作 */
function handleDelete(row, index) {
  const infoIds = row.id
  const _index = 1 + (index || 0)
  proxy.$modal
    .confirm('是否确认删除序号为："' + _index + '"的数据项?')
    .then(function () {
      return delInfo(infoIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'ec/alarm/export',
    {
      ...queryParams.value
    },
    `alarm_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['environmentInfoRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 初始化列表
getList()
</script>
<style lang="scss" scoped>
@import '@/assets/styles/card.scss';
.mr {
  margin-right: 20px;
}
.slider-demo-block {
  margin-left: 10px;
}
</style>
